package com.gsit.expenditure.domain;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;

@Entity
@Table(name = "customer")
@DiscriminatorValue("CA")
@NamedQueries({@NamedQuery (name= "Customer.findAll",query= "select customer from Customer customer")})
public class Customer extends BaseEntity {
	
	@NotEmpty(message="First Name Required")
	@NotNull(message ="First Name Required")
	@Pattern  (regexp="(^[a-zA-Z]{0,15})",message="Enter a-z only")    
	@Column(name = "firstName")
	private String firstName;
	
	@Pattern  (regexp="(^[a-zA-Z]{0,15})",message="Enter a-z only") 
	@NotEmpty(message ="Last Name Required")
	@Column(name = "lastName")
	private String lastName;

	@Valid
	@OneToOne(optional = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinColumn(name = "address_id", insertable=true)
	private CustomerAddress address;

	@Email(message="Enter correct email form")
	@NotEmpty(message ="Email Required")
	@Column(name = "email")
	private String email;

	@Pattern  (regexp="(^[0-9+-]{0,15})",message="Invalid characters,Enter only (0-9,+,-)") 
	@Column(name = "phone")
	private String phone;

	@NotEmpty(message ="Mobile Required")
	@Column(name = "mobile")
	@NumberFormat(style= Style.NUMBER)
	@Pattern(regexp ="([0-9]{10})", message = "Enter 10 digit number only")
	private String mobile;
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private Set<Bill> billsList ;
	
	@Transient
	private Boolean isDelete = Boolean.FALSE;
	
	@Transient
	private Boolean select = Boolean.FALSE;

//	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
//	@JoinColumn(name = "invoice_id")
//	private List<Invoice> invoices;
	
	public Boolean getSelect() {
		return select;
	}

	public void setSelect(Boolean select) {
		this.select = select;
	}

	@Transient
	public String getFullName() {
		StringBuilder fullName = new StringBuilder();
		Boolean isFirst = Boolean.TRUE;
		if (firstName != null && !firstName.trim().equals("")) {

			if (firstName != null && !firstName.trim().equals("")) {
				fullName.append(firstName);
				isFirst = Boolean.FALSE;
			}

			if (lastName != null && !lastName.trim().equals("")) {
				if (!isFirst)
					fullName.append(" ");
				fullName.append(lastName);

			}

		}
		return fullName.toString();
	}

	
	public String getFirstName() {
		return firstName;
	}
	
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Valid
	public CustomerAddress getAddress() {
		return address;
	}

	public void setAddress(CustomerAddress address) {
		this.address = address;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public Set<Bill> getBillsList() {
		return billsList;
	}

	public void setBillsList(Set<Bill> billsList) {
		this.billsList = billsList;
	}
	
	public Boolean getIsDelete() {
		return isDelete;
	}

	public void setIsDelete(Boolean isDelete) {
		this.isDelete = isDelete;
	}

	@Override
	public String toString() {
		return "Customer [firstName=" + firstName + ", lastName=" + lastName
				+ ", address=" + address + ", email=" + email + ", phone="
				+ phone + ", mobile=" + mobile + ", billsList=" + billsList
				+ ", isDelete=" + isDelete + ", select=" + select
				+ ", getId()=" + getId() + ", getCreatedDate()="
				+ getCreatedDate() + ", getModifiedDate()=" + getModifiedDate()
				+ ", getVersion()=" + getVersion() + ", getClass()="
				+ getClass() + ", hashCode()=" + hashCode() + ", toString()="
				+ super.toString() + "]";
	}

	
	
	
}
